package com.zengqingfa.springboot.redis.rest;

import com.zengqingfa.springboot.redis.service.RedisRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author zengqingfa
 * @className RedisRankController
 * @description
 * @create 2022/9/21 22:53
 */
@RestController
@RequestMapping("/redis/rank")
public class RedisRankController {
    @Autowired
    private RedisRankService redisService;

    /**
     * 测试redis记录keyword
     *
     * @param keyword 搜索关键词
     * @return
     */
    @GetMapping("/search")
    public Boolean search(@RequestParam("keyword") String keyword) {
        redisService.searchZincrby(keyword);
        return Boolean.TRUE;
    }

    /**
     * 测试redis查询指定范围的热词
     *
     * @param start 查询范围开始位置,从0开始
     * @param end   查询范围结束位置
     * @return
     */
    @GetMapping("/queryTopSearchHotWord")
    public Set<ZSetOperations.TypedTuple<String>> queryTopSearchHotWord(@RequestParam("start") Integer start,
                                                                        @RequestParam("end") Integer end) {
        Set<ZSetOperations.TypedTuple<String>> resultSet = redisService.queryTopSearchHotWord(start, end);
        return resultSet;
    }

    /**
     * 删除指定的key
     *
     * @param keyName keyName
     * @return
     */
    @DeleteMapping("/deleteKey")
    public Boolean deleteKey(@RequestParam("keyName") String keyName) {
        redisService.deleteKey(keyName);
        return Boolean.TRUE;
    }

}
